package com.haoshuang.zhouzhoubang.ui.page.order.rework.rework.compensate

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityCompensateBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderDetailBean
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.load

/**
 *  返工补偿
 */
class CompensateActivity : BaseVMActivity<CompensateViewModel, ActivityCompensateBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<CompensateActivity>()
        }
    }

    private var max_compensation_amount = 0.0
    private var detailBean: OrderDetailBean? = null
    private var orderId = ""

    override fun initView(savedInstanceState: Bundle?) {
        orderId = intent.getString("order_id")

        binding.titleBar.title = "补偿诚意金"
        binding.titleBar.leftImageView().click { finish() }

        binding.tvAll.clickWithTrigger {
            binding.etMoney.setText(detailBean?.max_compensation_amount)
        }

        binding.tvOk.clickWithTrigger {
            val money = binding.etMoney.text.toString().toDoubleOrNull()?:0.0
            if (money > max_compensation_amount){
                toast("补偿金额不能超过${max_compensation_amount}元")
                return@clickWithTrigger
            }

            if (money == 0.0){
                toast("补偿金额不能为0")
                return@clickWithTrigger
            }

            XPopup.Builder(this)
                .asCustom(
                    ConfirmDialog(
                    context = this,
                    title = "温馨提示",
                    content = "已和客户协商确定补偿\n${money}元诚意金",
                    cancelStr = "再考虑考虑",
                    confirmStr = "确认",
                    isHideCancel = false,
                    onCancel = {},
                    onConfirm = {
                        vm.masterCompensateMindAmount(orderId, money.toString())
                    })
                ).show()
        }
    }

    override fun initData() {
        vm.getServiceOrderDetail(orderId)
    }

    override fun startObserve() {
        vm.getServiceOrderDetailData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                detailBean = it?.data
                binding.rivImage.load(detailBean?.product_image)
                binding.tvTitleName.text = detailBean?.store_name
                binding.tvDesc.text = detailBean?.suk_unique
                max_compensation_amount = detailBean?.max_compensation_amount?.toDoubleOrNull() ?: 0.0

            }
        }

        vm.masterCompensateMindAmountData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                setResult(200)
                finish()
            }
        }
    }

}